package com.amumu.drama.common.modules.invitation.mapping;

import com.amumu.drama.common.base.mapstruct.BaseMapping;
import com.amumu.drama.common.base.mapstruct.ext.LocalDateTimeToStringMapping;
import com.amumu.drama.common.constant.InvitationTypeEnum;
import com.amumu.drama.common.dto.OrderStatisticsDTO;
import com.amumu.drama.common.modules.invitation.cache.InvitationOrderStatisticsCache;
import com.amumu.drama.common.modules.invitation.dto.resp.InvitationAnchorDetailResp;
import com.amumu.drama.model.InvitationAnchor;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.math.BigDecimal;

/**
 * @author sunzhuang
 * @date 2024/9/13 14:08
 * @slogan 天下风云出我辈, 一入代码岁月催
 * @desc todo
 */

@Mapper(componentModel = "spring")
public interface InvitationAnchorDetailRespMapping extends BaseMapping<InvitationAnchor, InvitationAnchorDetailResp>, LocalDateTimeToStringMapping {
    InvitationAnchorDetailRespMapping INSTANCE = Mappers.getMapper(InvitationAnchorDetailRespMapping.class);

    /**
     * 正向映射的后置处理，List映射会自动继承此配置
     *
     * @param src
     * @param dest
     */
    @Override
    default void afterConvertTo(InvitationAnchor src, InvitationAnchorDetailResp dest) {
//邀请码累计订单数
        OrderStatisticsDTO statistics = InvitationOrderStatisticsCache.INSTANCE.getInvitationOrderStatisticsFromCache(InvitationTypeEnum.ANCHOR.getCode(), src.getCode());
        if (statistics != null) {
            dest.setOrderCount(statistics.getOrderCount());
            dest.setTotalAmount(statistics.getTotalAmount() == null ? new BigDecimal(0) : statistics.getTotalAmount());
            dest.setInviteMemberCount(statistics.getInvitationMemberCount());
        }
    }
}
